package com.yalpuz.service.impl;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.yalpuz.entity.QUser;
import com.yalpuz.entity.User;
import com.yalpuz.repository.UserRepository;
import com.yalpuz.service.UserService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    /**
     * 测试查询a开头
     * @return
     */
    @Override
    public Page<User> list() {
        QPageRequest pageable = new QPageRequest(0, 10);
        User user = new User();
        user.setName("a");
        Example<User> userExample = Example.of(user, ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.startsWith()));
        return userRepository.findAll(userExample, pageable);
    }

    /**
     * 查询id大于20
     * @return
     */
    @Override
    public Page<User> list2() {
        QPageRequest pageable = new QPageRequest(0, 10);
        BooleanExpression expression = QUser.user.id.gt(20);
        return userRepository.findAll(expression, pageable);
    }


}
